package java44th.group.service.admin.comment;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.common.BasePageInfo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketComment;
import java44th.group.bean.po.MarketCommentExample;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.mapper.MarketCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @projectName: project-second
 * @package: java44th.group.service.admin.comment
 * @className: AdminCommentsService
 * @author: 杨浩祥
 * @description: TODO
 * @date: 2022/10/12 23:10
 * @version: 1.0
 */
@Service
public class AdminCommentsServiceImpl implements AdminCommentsService{

    @Autowired
    MarketCommentMapper commentMapper;

    @Override
    public BaseRespVo list(BasePageInfo info,Integer userId,Integer valueId) {
        MarketCommentExample example = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (info.getLimit() == 0){
            List<MarketComment> list = commentMapper.selectByExample(example);
            CommonData<MarketComment> marketCommentCommonData = new CommonData<>();
            marketCommentCommonData.setLimit(0);
            marketCommentCommonData.setPage(0);
            marketCommentCommonData.setPages(0);
            marketCommentCommonData.setTotal(list.size());
            marketCommentCommonData.setList(list);
            return BaseRespVo.ok(marketCommentCommonData);
        }
        PageHelper.startPage(info.getPage(), info.getLimit());
        if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null){
            criteria.andValueIdEqualTo(valueId);
        }
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        List<MarketComment> list = commentMapper.selectByExample(example);
        PageInfo<MarketComment> pageInfo = new PageInfo<>(list);
        CommonData<MarketComment> data = CommonData.data(pageInfo);
        return BaseRespVo.ok(data);
    }

    @Transactional
    @Override
    public BaseRespVo deleted(MarketComment marketComment) {
        marketComment.setDeleted(true);
        commentMapper.updateByPrimaryKey(marketComment);
        return BaseRespVo.ok();
    }
}
